package com.museum.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.museum.base.BaseResponse;
import com.museum.controller.APi.feignAPI.FeignAPIController;
import com.museum.core.utils.VeDate;
import com.museum.entity.DO.Clue;
import com.museum.entity.DO.ColProcess;
import com.museum.entity.DO.ColRegister;
import com.museum.entity.DO.RegisterProcess;
import com.museum.entity.DTO.ClueInfo;
import com.museum.entity.DTO.ColQuery;
import com.museum.entity.DTO.CommitCol;
import com.museum.entity.DTO.QueryCol;
import com.museum.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ruiwen
 * @since 2021-01-05
 */
@Service
public class RegisterClueService2Impl extends ServiceImpl<ColRegisterMapper, ColRegister> {
     @Autowired
     private FeignAPIController feignAPIController;
     @Autowired
     private ProcessMapper processService;
    @Autowired
    private RegisterProcessMapper registerProcessMapper;
    @Autowired
    private ColRegisterMapper colMapper;
     @Autowired
     private ClueMapper clueMapper;
    @Autowired
    private ColRegisterMapper clueMapper2;
     @Autowired
     private ProcessMapper processMapper;
     @Autowired
     private SortMapper sortMapper;
     @Autowired
     private YearMapper yearMapper;
     @Autowired
     private GradeMapper gradeMapper;
    /**
     * @param
     * @Desciption 提交征集 状态转换为 1/-1
     * @return
     */
    public int commitCol(CommitCol commitcol,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        BaseResponse<JSONObject> userMessageByUSerId = feignAPIController.getUserMessageByUSerId(token);
        JSONObject data = userMessageByUSerId.getData();
        String price_desc =  commitcol.getDesc();
        Integer status = commitcol.getStatus();
        Integer id = commitcol.getId();
       // ColProcess process = clueMapper.selectProcessById(id);
        RegisterProcess process = registerProcessMapper.selectProcessById(id);
        if(StringUtils.isEmpty(price_desc)){
            price_desc ="此人无留言.";
            process.setSecondDesc(price_desc);
        }else{
            process.setSecondDesc(commitcol.getDesc());
        }
        if(status==-1){
            process.setStatus(-1);
        }
        //registerProcessMapper.commitColPrice(id,status);
        process.setSecondStatus(status);
        process.setSecondTime(VeDate.getStringDate());
        process.setSecondUser((String)data.get("realName"));
        return  registerProcessMapper.updateById(process);
    }
    /**
     * @param
     * @Desciption 条件查询征集
     * @return
     */
    public JSONObject getAllColByPage(long current, long limit, ColQuery queryCol){
        HashMap<String,Object> map =  new HashMap<String,Object>();
        List<ColRegister> clueList = new ArrayList<>();
        String year =null;
        String colName=null;
        String colId=null;
        if(queryCol!=null){
            year = queryCol.getYear();
            colName = queryCol.getColName();
            colId = queryCol.getColId();
        }
        if (!StringUtils.isEmpty(year)) {
            int yearId = yearMapper.selectIdByName(year);
            map.put("yearId","%"+yearId+"%");
        }
        if (!StringUtils.isEmpty(colName)) {
            map.put("colName","%"+colName+"%");
        }
        if (!StringUtils.isEmpty(colId)) {
            map.put("colId","%"+colId+"%");
        }
        map.put("operate_status",1);
        map.put("current", (current - 1) * limit);
        map.put("limit",limit);
        //System.out.println("map=="+map);
        List<ColRegister> list = colMapper.selectColByPage(map);
        //System.out.println("集合=="+list);
        for (ColRegister clue : list) {
            int clueId = clue.getId();
            int yearId = clue.getYearId();
            int gradeId = clue.getGradeId();
            String yearName = yearMapper.selectNameById(yearId);
            String gradeName = gradeMapper.selectNameById(gradeId);
            Integer status = registerProcessMapper.getStatus(clueId);
            clue.setYearName(yearName);
            clue.setGradeName(gradeName);
            if(registerProcessMapper.isOperateStauts2(clueId)&&status!=-1){
                //System.out.println("此时的id="+clueId);
                clueList.add(clue);
            }
        }
        int count = colMapper.selectCount(null);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",count);
        jsonObject.put("ClueList",clueList);
        return jsonObject;
    }
}
